var setting = {
	view: {
		showLine: true,
		showIcon: false,
		selectedMulti: true,
		dblClickExpand: false,
		addDiyDom: addDiyDom
	},
	async: {
		enable: true,
		//url:"http://dayang.fast.com:8087/dywebpageserver/HogeController/getHogeColumn",
//		url:cmedit.global.cmsPath + "HogeController/getHogeColumn",
		url:cmedit.global.cmsPath + "HogeController/getHogeColumn",
		autoParam:["id=fid"],
		otherParam:{},
		dataFilter: filter
	},
	callback: {
		beforeClick: beforeClick,
		beforeAsync: beforeAsync,
		onAsyncError: onAsyncError,
		onAsyncSuccess: onAsyncSuccess,
		onClick:zTreeOnClick//节点被点击的事件回调函数	  
		
	},
	check: {
        enable: true,
        chkboxType : {"Y": "", "N": ""},//勾选 checkbox 对于父子节点的关联关系。[setting.check.enable = true 且 setting.check.chkStyle = "checkbox" 时生效]  
        chkStyle : "radio",//勾选框类型(checkbox 或 radio）[setting.check.enable = true 时生效]  
    	radioType: "all"   //对所有节点设置单选
    },
    data: {
		simpleData: {
			enable: true
		}
	}
};

function filter(treeId, parentNode, childNodes) {
	var trreChildNodes=[];
	 if(childNodes.status){
		 if (!childNodes.data) return null;
		 var childNodesdata=childNodes.data;
			for (var i=0, l=childNodesdata.length; i<l; i++) {
				var node={};
				node.name=childNodesdata[i].columnName;
				node.id=childNodesdata[i].columnId;
				node.pId=childNodesdata[i].fatherId;
				node.isParent=childNodesdata[i].isLast;
				trreChildNodes[i] =node;
			}
	 }else{
	 }
	
	return trreChildNodes;
}
function beforeClick(treeId, treeNode) {
	return true;
}
var log, className = "dark";
function beforeAsync(treeId, treeNode) {
	className = (className === "dark" ? "":"dark");
	
	showLog("[ "+getTime()+" beforeAsync ]&nbsp;&nbsp;&nbsp;&nbsp;" + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
	return true;
}
function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
	showLog("[ "+getTime()+" onAsyncError ]&nbsp;&nbsp;&nbsp;&nbsp;" + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );
}
function onAsyncSuccess(event, treeId, treeNode, msg) {
	showLog("[ "+getTime()+" onAsyncSuccess ]&nbsp;&nbsp;&nbsp;&nbsp;" + ((!!treeNode && !!treeNode.name) ? treeNode.name : "root") );

	var webPage = $(window.top.document).find('iframe[name="main-iframe"]').contents().find('iframe[id="main-iframe-right"]').contents();
	var tabId=webPage.contents().find('input[id="manuscriptId_select"]').val();
	var columnId = webPage.contents().find('input[id="orign_hjcolumnid_'+tabId+'"]').val();
	if(columnId!=null&&columnId!=""){
		var columnIdArr = new Array();
		if(columnId.indexOf(",")!=-1){
			columnIdArr = columnId.split(",");
		}else{
			columnIdArr.push(columnId);
		}
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		for(var i=0;i<columnIdArr.length;i++){
			var node = zTree.getNodeByParam("id",columnIdArr[i]);
			if(node!=null){
				node.checked = true;
				zTree.updateNode(node); 
			}
		}
	}
}

function showLog(str) {
	if (!log) log = $("#log");
	log.append("<li class='"+className+"'>"+str+"</li>");
	if(log.children("li").length > 8) {
		log.get(0).removeChild(log.children("li")[0]);
	}
}
function getTime() {
	var now= new Date(),
	h=now.getHours(),
	m=now.getMinutes(),
	s=now.getSeconds(),
	ms=now.getMilliseconds();
	return (h+":"+m+":"+s+ " " +ms);
}

function refreshNode(e) {
	var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	type = e.data.type,
	silent = e.data.silent,
	nodes = zTree.getSelectedNodes();
	if (nodes.length == 0) {
		// alert(window.top.i18n("CMS_choose_node"));//请先选择一个父节点
	}
	for (var i=0, l=nodes.length; i<l; i++) {
		zTree.reAsyncChildNodes(nodes[i], type, silent);
		if (!silent) zTree.selectNode(nodes[i]);
	}
}
function addDiyDom(treeId, treeNode) {
	var spaceWidth = 5;
	var switchObj = $("#" + treeNode.tId + "_switch"),
	icoObj = $("#" + treeNode.tId + "_ico");
	switchObj.remove();
	icoObj.before(switchObj);
	
	if(treeNode.isParent){
		$("#" + treeNode.tId + "_check").remove();
		$("#" + treeNode.tId).css('margin-left','13px');
	}
	if (treeNode.level > 1) {
		var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level)+ "px'></span>";
		switchObj.before(spaceStr);
	}
}

function zTreeOnClick(event,treeId,treeNode){
	$("input[name='columnName']").val(treeNode.name);
	$("input[name='columnId']").val(treeNode.id);
}
function ztreeConfirm(){
	var columnId=$("input[name='columnId']").val();
	var columnName=$("input[name='columnName']").val();
	columnId = "";
	columnName = "";
	var treeObj=$.fn.zTree.getZTreeObj("treeDemo");
    var nodes=treeObj.getCheckedNodes(true);
    for(var i=0;i<nodes.length;i++){
    	columnId += nodes[i].id + ",";
    	columnName += nodes[i].name + ",";
    }
    if(columnId!=""){
    	columnId = columnId.substring(0, columnId.lastIndexOf(","));
    }
    if(columnName!=""){
    	columnName = columnName.substring(0, columnName.lastIndexOf(","));
	}
    var newMainGuid = window.sessionStorage.getItem("newMainGuid");
	// $("#newsColum").val(columnName)
	// window.top.document.getElementById("newsColum").contentWindow.document.getElementById('main-iframe-right').contentWindow.addColumn(columnId,columnName);
	// window.top.document.getElementById('main-iframe').contentWindow.document.getElementById('main-iframe-right').contentWindow.addColumn(columnId,columnName);
	window.top.document.getElementById('iframe_' + newMainGuid).contentWindow.document.getElementById('newsColum').value=columnName;
	window.top.document.getElementById('iframe_' + newMainGuid).contentWindow.document.getElementById('newsColum').name=columnId;
	// parent.close();
}

/**
*  页面文字国际化
*/
function cms_international(){
	$('#dict_cms_title').html(window.top.i18n('CMS_title'));//CMS栏目选择
	$('#dict_cms_column').html(window.top.i18n('CMS_choose_column'));//请选择新媒体栏目:
	$('#dict_cms_confirm').attr("value",window.top.i18n('public_confirm'));//确定
	$('#dict_cms_cancel').attr("value",window.top.i18n('public_cancel'));//取消
}

$(document).ready(function(){
	// cms_international();
	var hogeColumnFlag = window.sessionStorage.getItem("hogeColumnFlag");
	if(cmedit.global.isNotEmpty(hogeColumnFlag) && hogeColumnFlag == '1'){
		var treeObj = $("#treeDemo");
		$.fn.zTree.init($("#treeDemo"), setting);
		$("#refreshNode").bind("click", {type:"refresh", silent:false}, refreshNode);
		$("#refreshNodeSilent").bind("click", {type:"refresh", silent:true}, refreshNode);
		$("#addNode").bind("click", {type:"add", silent:false}, refreshNode);
		$("#addNodeSilent").bind("click", {type:"add", silent:true}, refreshNode);

		treeObj.hover(function () {
			if (!treeObj.hasClass("showIcon")) {
				treeObj.addClass("showIcon");
			}
		}, function() {
			treeObj.removeClass("showIcon");
		});
	}
});